﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using Way.Bus.WMS.Model;
using Way.Contract.Data;
using System.Collections;
using Way.Contract.MVC;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using Way.Contract.Data.ControllerData;
using Way.EAP.DataAccess.Regulation;
using Way.EAP.DataAccess.Data;

namespace Way.Bus.WMS.Controllers
{
    public class CheckOrderController : BusinessController
    {
        /// <summary>
        /// 描述：盘点单管理控制器
        /// 作者：路聪
        /// 创建日期:2012-12-25
        /// </summary>
        public CheckOrderController() : base(new CheckOrder())
        {
        }

        public CheckOrderController(IModel model) : base(model)
        {
        }

        //是否启用审核
        public override bool IsReviewedState
        {
            get
            {
                return true;
            }
        }

        //是否启用提交
        public override bool IsSubmit
        {
            get
            {
                return true;
            }
        }
        protected override string SubmitName
        {
            get
            {
                return "启动盘点";
            }
        }
        protected override string ReviewedName
        {
            get
            {
                return "结束盘点";
            }
        }
        //是否启用回收站
        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }

        public override bool IsPrint
        {
            get
            {
                return false;
            }
        }

        //显示在菜单
        protected override string OnControllerName()
        {
            return "盘点单管理";
        }

        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/WMS/Check/CheckOrder.htm";
        }

        //开发人
        protected override string OnGetAuthor()
        {
            return "";
        }

        //说明
        protected override string OnGetControllerDescription()
        {
            return "盘点单管理";
        }

        #region 初始化数据方法

        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            ActionCommand[] coms = base.OnInitCommand(commands);
            //获取所有按钮集合
            foreach (ActionCommand ac in coms)
            {
                //if (ac.command == "add")
                //{
                //    ac.name = "生成下级盘点单";
                //}
                if (ac.command == "UnReviewedData")
                    ac.visible = false;
            }
            return coms;
        }

        protected override void OnInitViewChildItem(Way.Contract.Data.ModelData data, Way.Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (item.IsField("CheckOrderType"))
                item.disabled = false;
            if (item.IsField("code"))
                item.disabled = true;
            if (item.IsField("IsDisable"))
                item.visible = false;
            if (data.name.EndsWith("CheckOrderMaterials"))
            {
                if (item.IsField("CREATEDBY"))
                    item.title = "库管";
                if (item.IsField("CREATIONDATE"))
                    item.title = "盘点时间";
                if (item.IsField("Code") || item.IsField("ISDISABLE") || item.IsField("ISSUBMITED") || item.IsField("SUBMITEDBY")
                    || item.IsField("SUBMITEDDATE") || item.IsField("REMARK") || item.IsField("ISREVIEWED") || item.IsField("REVIEWEDBY") || item.IsField("REVIEWEDDATE"))
                    item.visible = false;
                if (item.IsField("MATERIALCODE"))
                {
                    item.foreign.rowdisplay.Add("CODE", "NEWMCODE");
                    item.foreign.rowdisplay.Add("FMODEL", "NEWFMODEL");
                }
            }
        }

        #endregion

        #region 外键处理
        ///<summary>
        ///选择仓库和货区及货位的事件
        ///</summary>
        ///<param name="model"></param>
        ///<param name="item"></param>
        protected override void OnForeignLoading(IModel model, loadItem item)
        {
            base.OnForeignLoading(model, item);
            List<SQLWhere> list = new List<SQLWhere>();
            list.AddRange(item.whereList);
            ///只能选择仓库或货区
            if (this.fdata.filedname == "WAREHOUSENAME")
            {
                SQLWhere w = new SQLWhere();
                w.ColumnName = "WHTYPE";
                w.Symbol = "in";
                w.Value = "(0,1)";
                list.Add(w);
            }
            if (this.fdata.filedname == "PDEPOTWBS")
            {
                if (this.ActionItem["WAREHOUSENAME"] == null || this.ActionItem["WAREHOUSENAME"].ToString() == "")
                    throw new Exception("异常：未选择盘点位置！");
                string id = this.ActionItem["WAREHOUSENAME"].ToString();
                SQLWhere w = new SQLWhere();
                w.ColumnName = "WHTYPE";
                w.Value = "2";
                list.Add(w);
                SQLWhere w1 = new SQLWhere();
                w1.StartFH = "(";
                w1.ColumnName = "PARENTID";
                w1.Value = id;
                SQLWhere w2 = new SQLWhere();
                w2.Relation = "or";
                w2.EndFH = ")";
                w2.ColumnName = "PARENTID";
                w2.Symbol = "in";
                w2.Value = "(select ID from Acc_WMS_WareHouse where PARENTID=" + id + ")";
                list.Add(w1);
                list.Add(w2);
            }
            item.whereList = list.ToArray();
        }

        protected override void OnForeignIniting(IModel model, InitData data)
        {
            if (model is CheckOrder)
            {
                //序号、编码code、名称FNAME、全名FFULLNAME、大类ClassID，基本计量单位FUNITID、批次管理BATCH、状态STATUS、是否迅速超出ISOVERIN是否禁用IsDisable、是否质检ISCHECKPRO、备注Remark
                if (this.fdata.filedname == "MATERIALCODE")
                {
                    foreach (ItemData d in data.modeldata.childitem)
                    {
                        d.visible = false;
                        if (d.IsField("Code"))
                        {
                            d.visible = true;
                            d.title = "商品编码";
                        }
                        if (d.IsField("FNAME"))
                            d.visible = true;
                        if (d.IsField("FFULLNAME"))
                            d.visible = true;
                        if (d.IsField("ClassID"))
                            d.visible = true;
                        if (d.IsField("FUNITID"))
                            d.visible = true;
                        if (d.IsField("BATCH"))
                            d.visible = true;
                        if (d.IsField("STATUS"))
                            d.visible = true;
                        if (d.IsField("ISOVERIN"))
                            d.visible = true;
                        if (d.IsField("IsDisable"))
                            d.visible = true;
                        if (d.IsField("Remark"))
                            d.visible = true;
                        if (d.IsField("CommodityType"))
                            d.visible = true;
                    }
                }
                if (this.fdata.filedname == "WAREHOUSENAME")
                {
                    foreach (ItemData d in data.modeldata.childitem)
                    {//序号、仓库编码Code、名称WAREHOUSENAME、类型WHTYPE、上级PARENTID、是否禁用IsDisable、备注Remark
                        d.visible = false;
                        if (d.IsField("ROWINDEX"))
                        {
                            d.visible = true;
                        }
                        if (d.IsField("Code"))
                        {
                            d.visible = true;
                            d.title = "仓库编码";
                        }
                        if (d.IsField("WAREHOUSENAME"))
                            d.visible = true;
                        if (d.IsField("WHTYPE"))
                            d.visible = true;
                        if (d.IsField("PARENTID"))
                            d.visible = true;
                        if (d.IsField("IsDisable"))
                            d.visible = true;
                        if (d.IsField("Remark"))
                            d.visible = true;
                    }
                }
                if (this.fdata.filedname == "PDEPOTWBS")
                {
                    foreach (ItemData d in data.modeldata.childitem)
                    {
                        d.visible = false;
                        if (d.IsField("code") || d.IsField("Parentid"))
                            d.visible = true;

                    }
                }
            }
        }

        #endregion
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        protected override void OnAdding(Way.Contract.MVC.ControllerBase.SaveEvent item)
        {
            //Merger(item);
            ValidateItem(item);
            base.OnAdding(item);

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="item"></param>
        protected override void OnEditing(Way.Contract.MVC.ControllerBase.SaveEvent item)
        {
            // Merger(item);
            ValidateItem(item);
            CheckOrder order = item.Item as CheckOrder;
            if (!order.IsSubmited)
                base.OnEditing(item);
            else
            {
                item.Breakoff = true;
                ValiItems((CheckOrder)item.Item);
                order.CheckOrderMaterials.Save();
            }
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="info"></param>
        protected override void OnReviewedData(BasicInfo info)
        {
            CheckOrder co = base.getinfo(this.ActionItem["ID"].ToString()) as CheckOrder;
            co.CheckOrderMaterials.DataAction = this.model.GetDataAction();
            co.CheckOrderMaterials.GetData();
            if (co.CheckOrderMaterials.Count == 0)
            {
                throw new Exception("异常：“盘点明细不能为空！");
            }
            base.OnReviewedData(info);

        }

        #region 验证数据有效性
        /// <summary>
        /// 验证数据正确性
        /// </summary>
        /// <param name="item"></param>
        public virtual void ValidateItem(ControllerBase.SaveEvent item)
        {
            CheckOrder bc = item.Item as CheckOrder;
            ValiDoubleName(bc);
            ValiDepots(bc);
            ValiMaterialsClass(bc);
        }

        /// <summary>
        /// 验证盘点名称是否重复
        /// </summary>
        /// <param name="cho"></param>
        public virtual void ValiDoubleName(CheckOrder cho)
        {
            EntityList<CheckOrder> co = new EntityList<CheckOrder>(this.model.GetDataAction());
            if (cho.ID != 0)
            {
                co.GetData("ORDERNAME = '" + cho.ORDERNAME + "' and id<>" + cho.ID + "");
            }
            else
                co.GetData("ORDERNAME='" + cho.ORDERNAME + "'");
            if (co.Count > 0)
            {
                throw new Exception("异常：盘点名称重复");
            }
        }

        /// <summary>
        /// 验证盘点位置
        /// </summary>
        /// <param name="cho"></param>
        public virtual void ValiDepots(CheckOrder cho)
        {
            if (cho.WAREHOUSENAME != null) ///盘点位置不为空
            {
              
            }
            else
            {
                throw new Exception("异常：盘点位置不能为空！");
            }
        }

        /// <summary>
        /// 验证明细
        /// </summary>
        /// <param name="cho"></param>
        public virtual void ValiItems(CheckOrder cho)
        {
            foreach (var cm in cho.CheckOrderMaterials)
            {
                if (string.IsNullOrEmpty(cm.MATERIALCODE))
                    throw new Exception("异常：商品不能为空！");
                if (string.IsNullOrEmpty(cm.PDEPOTWBS))
                    throw new Exception("异常：货位不能为空！");
                else
                {
                    int d = 0;
                    if (!int.TryParse(cm.PDEPOTWBS, out d))
                    {
                        string hw = cm.PDEPOTWBS;
                        cm.PDEPOTWBS = WareHouse.GetDepotwesID(int.Parse(cho.WAREHOUSENAME), cm.PDEPOTWBS, this.model.GetDataAction()).ToString();
                        if (cm.PDEPOTWBS == "0")
                            throw new Exception("异常：" + hw + "在系统中未找到！");
                    }
                    else
                    {
                        cm.PDEPOTWBS = WareHouse.GetDepotwesID(int.Parse(cho.WAREHOUSENAME), d, this.model.GetDataAction()).ToString();
                        if (cm.PDEPOTWBS == "0")
                            throw new Exception("异常：" + d + "在系统中未找到！");
                    }
                }
                Materials m = cm.GetForeignObject<Materials>(this.model.GetDataAction());
                if (m.BATCH)
                {
                    if (string.IsNullOrEmpty(cm.PBATCHNO))
                        throw new Exception("异常：商品" + m.FNAME + "必须录入批次！");
                }
                if (cm.PNUM <= 0)
                    throw new Exception("异常：“" + cm.GetAppendPropertyValue("MATERIALCODE") + "”数量必须大于0");
                cm.Createdby = this.user.ID;
                cm.Creationdate = DateTime.Now;
            }
        }

        /// <summary>
        /// 验证产品大类
        /// </summary>
        /// <param name="cho"></param>
        public virtual void ValiMaterialsClass(CheckOrder cho)
        {
            //foreach (var i in cho.CheckOrderMaterials)
            //{
            //    Materials ms = i.GetForeignObject<Materials>(this.model.GetDataAction());
            //    if (cho.ClassID != 0 && cho.ClassID != null)
            //    {
            //        if (ms.ClassID != cho.ClassID)
            //        {
            //            throw new Exception("异常：“" + ms.FNAME + "”所属大类与单据不匹配");
            //        }
            //    }
            //}
        }
        #endregion

        #region 生成盘点差异
        public void pdcy(CheckOrder co)
        {
            StockInfoMaterials sim = new StockInfoMaterials();
            string sql = "select Code cid,DEPOTWBS did,BATCHNO batch,sum(num) num from "+sim.ToString() //按盘点位置获取库存数据
                    + " where (WAREHOUSEID=" + co.WAREHOUSENAME + " or DEPOTWBS in (select ID from Acc_WMS_WareHouse where PARENTID=" + co.WAREHOUSENAME + ")) group by Code,DEPOTWBS,BATCHNO";
            CheckOrderMaterials com = new CheckOrderMaterials();
            string csql = "select MATERIALCODE cid,PDEPOTWBS did,PBATCHNO batch,sum(PNUM) num  from " + com.ToString()
                       + " where PARENTID=" + co.ID + " group by MATERIALCODE,PDEPOTWBS,PBATCHNO";
            IDataAction action = this.model.GetDataAction();
            DataTable kc = action.GetDataTable(sql);
            kc.Columns.Add("pdnum");
            kc.Columns.Add("tznum");
            foreach (DataRow r in kc.Rows)
            {
                r["tznum"] = "-" + r["num"].ToString();
                r["pdnum"] = "0";
            }
            kc.PrimaryKey = new DataColumn[] { kc.Columns[0], kc.Columns[1], kc.Columns[2] };
            DataTable pd = action.GetDataTable(csql);
           
            foreach (DataRow r in pd.Rows)
            {
                DataRow rr = kc.Rows.Find(new object[] { r["cid"], r["did"], r["batch"] });
                if (rr != null)
                {
                    rr["tznum"] = Convert.ToDouble(r["num"].ToString()) + Convert.ToDouble(rr["tznum"].ToString());
                    rr["pdnum"] = r["num"].ToString();
                }
                else
                {
                    DataRow rrr = kc.NewRow();
                    foreach (DataColumn c in r.Table.Columns)
                        rrr[c.ColumnName] = r[c];
                    rrr["pdnum"] = r["num"];
                    rrr["num"] = 0;
                    rrr["tznum"] = r["num"];
                    kc.Rows.Add(rrr);
                }
            }
            kc.Columns.Add("cknum");
            kc.Columns.Add("rknum");
            if (co.CheckOrderType == "2")//动态盘点
            {
                string cksql = "select b.MATERIALCODE cid,b.DEPOTWBS did,b.BATCHNO batch,sum(num) num from Acc_WMS_OutOrder a,Acc_WMS_OutOrderMaterials b"
                                + " where a.ID=b.PARENTID and a.IsSubmited=1 and a.Submiteddate >='" + co.Submiteddate + "' and a.Submiteddate<='" + co.Revieweddate + "'"
                                + " and (a.TOWHNO=" + co.WAREHOUSENAME + " or b.DEPOTWBS in (select ID from Acc_WMS_WareHouse where PARENTID=" + co.WAREHOUSENAME + "))"
                                + " group by b.MATERIALCODE,b.DEPOTWBS,b.BATCHNO";
              
                string rksql = "select b.MATERIALCODE cid,b.DEPOTWBS did,b.BATCHNO batch,sum(num) num from Acc_WMS_inOrder a,Acc_WMS_inOrderMaterials b"
                               + " where a.ID=b.PARENTID and a.IsSubmited=1 and a.Submiteddate >='" + co.Submiteddate + "' and a.Submiteddate<='" + co.Revieweddate + "'"
                               + " and (a.TOWHNO=" + co.WAREHOUSENAME + " or b.DEPOTWBS in (select ID from Acc_WMS_WareHouse where PARENTID=" + co.WAREHOUSENAME + "))"
                               + " group by b.MATERIALCODE,b.DEPOTWBS,b.BATCHNO";
                DataTable ck = action.GetDataTable(cksql);
               
                foreach (DataRow r in ck.Rows)
                {
                   DataRow rr= kc.Rows.Find(new object[] { r["cid"], r["did"], r["batch"] });
                   if (rr != null)
                   {
                       rr["tznum"] = Convert.ToDouble(r["num"].ToString()) + Convert.ToDouble(rr["tznum"].ToString());
                       rr["cknum"] = r["num"];
                   }
                   else
                   {
                       DataRow rrr = kc.NewRow();
                       foreach (DataColumn c in r.Table.Columns)
                           rrr[c.ColumnName] = r[c];
                       rrr["cknum"] = r["num"];
                       rrr["num"] = 0;
                       rrr["tznum"] = r["num"];
                       kc.Rows.Add(rrr);
                   }
                }
                DataTable rk = action.GetDataTable(rksql);
               
                foreach (DataRow r in rk.Rows)
                {
                    DataRow rr = kc.Rows.Find(new object[] { r["cid"], r["did"], r["batch"] });
                    if (rr != null)
                    {
                        rr["tznum"] = Convert.ToDouble(rr["tznum"].ToString()) - Convert.ToDouble(r["num"].ToString());
                        rr["rknum"] = r["num"];
                    }
                    else
                    {
                        DataRow rrr = kc.NewRow();
                        foreach (DataColumn c in r.Table.Columns)
                            rrr[c.ColumnName] = r[c];
                        rrr["rknum"] = r["num"];
                        rrr["num"] = 0;
                        rrr["tznum"] = "-" + r["num"].ToString();
                        kc.Rows.Add(rrr);
                    }
                }
            }
          
            Difference dif = new Difference();
            dif.TOWHNO = co.WAREHOUSENAME;
            dif.SourceID = co.ID.ToString();
            dif.SourceController = this.ToString();
            dif.SourceCode = co.Code;
            dif.Code = new BillNumberController().GetBillNo(this);
            dif.Createdby = this.user.ID;
            dif.Creationdate = DateTime.Now;
            string insertSQL = "insert into Acc_WMS_DifferenceMaterials(MATERIALCODE,NEWMCODE,NEWFMODEL,PBATCHNO,PDEPOTWBS,PNUM,CNUM,KNUM,PARENTID,STAY4,STAY5,IsDisable,IsDelete)"
                                +"select ID,Code,FMODEL,'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',0,0 from Acc_Bus_BusinessCommodity where id={0}";
            action.StartTransation();
            EntityList<Difference> list = new EntityList<Difference>();
            list.Add(dif);
            IEntityList l=list;
            l.Save(action);
            foreach(DataRow r in kc.Rows)
            {
               action.Execute(string.Format(insertSQL,r["cid"],r["batch"],r["did"],r["pdnum"],r["tznum"],r["num"],dif.ID,r["cknum"],r["rknum"]));
            }
            action.Commit();
            action.EndTransation();
        }
        [ActionCommand(name = "生成盘点差异", title = "生成盘点差异表", index = 6, isalert = true, icon = "icon-search")]
        public void SetViewRDLC()
        {
            CheckOrder co = this.ActionItem as CheckOrder;
            if (co != null)
                pdcy(co);
        }
        //[ActionCommand(name = "生成盘点差异", title = "生成盘点差异表", index = 6, isalert = true, icon = "icon-search")]
        //public void SetViewRDLC1()
        //{
        //    CheckOrder iv = base.getinfo(this.ActionItem["ID"].ToString()) as CheckOrder;
        //    EntityList<CheckOrder> coList = new EntityList<CheckOrder>(this.model.GetDataAction());
        //    coList.GetData("id='" + iv.ID + "'");

        //    if (!iv.IsSubmited)
        //    {
        //        throw new Exception("异常：单据未提交，不能生成盘点差异！");
        //    }
        //    if (!iv.IsReviewed)
        //    {
        //        throw new Exception("异常：单据未审核，不能生成盘点差异！");
        //    }
        //    EntityList<Difference> dfList1 = new EntityList<Difference>(this.model.GetDataAction());
        //    dfList1.GetData("SourceCode='" + iv.Code + "'");
        //    if (dfList1.Count > 0)
        //    {
        //        throw new Exception("异常：已生成完成盘点差异，不能重复生成盘点差异！");
        //    }


        //    iv.CheckOrderMaterials.DataAction = this.model.GetDataAction();
        //    iv.CheckOrderMaterials.GetData();
        //    StockInfoMaterials sim = new StockInfoMaterials();
        //    //EntityList<StockInfoMaterials> infoList = new EntityList<StockInfoMaterials>(this.model.GetDataAction());
        //    DataTable infoList = null;
        //    string infoListSql = "select * from " + sim.ToString() + " where 1=1";
        //    ///创建盘点差异单据
        //    EntityList<Difference> dfList = new EntityList<Difference>(this.model.GetDataAction());

        //    Difference dif = new Difference();
        //    dif.TOWHNO = iv.WAREHOUSENAME;
        //    dif.SourceID = iv.ID.ToString();
        //    dif.SourceController = this.ToString();
        //    dif.SourceCode = iv.Code;
        //    dif.Code = new BillNumberController().GetBillNo(this);
        //    dif.Createdby = this.user.ID;
        //    dif.Creationdate = DateTime.Now;
        //    dfList.Add(dif);
        //    for (int i = 0; i < iv.CheckOrderMaterials.Count; i++)
        //    {
        //        var temp = iv.CheckOrderMaterials[i];
        //        ///根据盘点明细比对库存
        //        ////获取库存项
        //        infoListSql += "and Code='" + temp.MATERIALCODE + "' and DEPOTWBS='" + temp.PDEPOTWBS + "'";
        //        if (temp.PBATCHNO != "")
        //        {
        //            infoListSql += " and BATCHNO='" + temp.PBATCHNO + "'";
        //        }
        //        if (temp.PORTCODE != "")
        //        {
        //            infoListSql += " and PORTCODE='" + temp.PORTCODE + "'";
        //        }

        //        infoList = this.model.GetDataAction().GetDataTable(infoListSql);
        //        ////如果是动态盘点就执行以下
        //        if (iv.CheckOrderType == "2")
        //        {
        //            DataTable dtInOrder = GetInOrder(iv.Submiteddate, temp);
        //            if (dtInOrder.Rows.Count > 0)
        //            {
        //                infoList.Rows[0]["NUM"] = Convert.ToInt32(infoList.Rows[0]["NUM"]) -
        //                                          Convert.ToInt32(dtInOrder.Rows[0]["NUM"]);
        //            }
        //            DataTable dtOutOrder = GetOutOrder(iv.Submiteddate, temp);
        //            if (dtOutOrder.Rows.Count > 0)
        //            {
        //                infoList.Rows[0]["NUM"] = Convert.ToInt32(infoList.Rows[0]["NUM"]) +
        //                                          Convert.ToInt32(dtOutOrder.Rows[0]["NUM"]);
        //            }
        //        }
        //        dfList.Save();
        //        DifferenceMaterials dm = new DifferenceMaterials();
        //        ///设置盘点差异表，盘点表字段值
        //        // dm.PARENTID = bc.ID;
        //        dm.MATERIALCODE = temp.MATERIALCODE;
        //        dm.NEWMCODE = temp.NEWMCODE;
        //        dm.NEWFMODEL = temp.NEWFMODEL;
        //        dm.PBATCHNO = temp.PBATCHNO;
        //        dm.PNUM = temp.PNUM;
        //        dm.PDEPOTWBS = temp.PDEPOTWBS;
        //        dm.PORTCODE = temp.PORTCODE;
        //        if (infoList.Rows.Count > 0)
        //        {
        //            ///设置盘点差异表，库存表字段值
        //            dm.KMATERIALCODE = infoList.Rows[0]["Code"].ToString();
        //            dm.KMCODE = infoList.Rows[0]["MCODE"].ToString();
        //            dm.KBATCHNO = infoList.Rows[0]["BATCHNO"].ToString();
        //            dm.KPDEPOTWBS = Convert.ToInt32(infoList.Rows[0]["DEPOTWBS"]);
        //            dm.KNUM = Convert.ToDouble(infoList.Rows[0]["NUM"]);
        //        }
        //        else
        //        {
        //            dm.KMATERIALCODE = "";
        //            dm.KMCODE = "";
        //            dm.KBATCHNO = "";
        //            dm.KPDEPOTWBS = 0;
        //            dm.KNUM = 0;
        //            dm.KPDEPOTWBS = 0;
        //        }
        //        ///设置差异和调整数量
        //        dm.CNUM = dm.PNUM - dm.KNUM;
        //        dif.Materials.Add(dm);
        //        dfList.Save();

        //    }
        //}



        /// <summary>
        /// 根据盘点单提交日期查询此日期以后的所有产品的审核入库数量
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private DataTable GetInOrder(DateTime dt, CheckOrderMaterials cm)
        {
            //// --根据盘点单提交日期查询此日期以后的所有产品的审核入库数量
            string sql = "SELECT sim.MATERIALCODE,SUM(sim.NUM) AS num FROM dbo.Acc_WMS_InOrder " +
                         "si INNER join dbo.Acc_WMS_InOrderMaterials sim ON si.ID = sim.PARENTID" +
                         " WHERE si.Revieweddate>'" + dt + "' and  sim.MATERIALCODE='" + cm.MATERIALCODE + "' and sim.DEPOTWBS='" + cm.PDEPOTWBS + "' " +
                         " and sim.BATCHNO='" + cm.PBATCHNO + "'" +
                         "GROUP BY sim.MATERIALCODE,sim.FMODEL,sim.MCODE,sim.DEPOTWBS,sim.BATCHNO";
            return this.model.GetDataAction().GetDataTable(sql);
        }

        /// <summary>
        /// 根据盘点单提交日期查询此日期以后的所有产品的审核出库数量
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private DataTable GetOutOrder(DateTime dt, CheckOrderMaterials cm)
        {

            ////--根据盘点单提交日期查询此日期以后的所有产品的审核出库数量
            string sql = "SELECT sim.MATERIALCODE,SUM(sim.NUM) AS num FROM dbo.Acc_WMS_OutOrder " +
                         "si INNER join dbo.Acc_WMS_OutOrderMaterials sim ON si.ID = sim.PARENTID" +
                         " WHERE si.Revieweddate<'" + dt + "' and  sim.MATERIALCODE='" + cm.MATERIALCODE + "' and sim.DEPOTWBS='" + cm.PDEPOTWBS + "' " +
                         " and sim.BATCHNO='" + cm.PBATCHNO + "'" +
                         "GROUP BY sim.MATERIALCODE,sim.FMODEL,sim.MCODE,sim.DEPOTWBS,sim.BATCHNO";
            return this.model.GetDataAction().GetDataTable(sql);
        }

        #endregion

    }
}
